/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumxml;

import java.io.FileInputStream;

import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Channel;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives for XPath queries.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class XPath {

    /**
     * No instance of this class.
     */
    private XPath() {
    } // end empty constructor

    /**
     * Constructs an xpath factory.
     * @param ctxt context
     * @param unit ignored
     * @return a new xpath factory
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpathfactory_make(final CodeRunner ctxt,
                                                     final Value unit)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(XPathFactory.newInstance());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpathfactory_make(CodeRunner, Value)'

    /**
     * Sets the value of a feature flag.
     * @param ctxt context
     * @param fact factory
     * @param name feature name
     * @param value feature value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpathfactory_set_feature(final CodeRunner ctxt,
                                                            final Value fact,
                                                            final Value name,
                                                            final Value value)
        throws Fail.Exception {
        try {
            final XPathFactory f = (XPathFactory) fact.asBlock().asCustom();
            f.setFeature(name.asBlock().asString(), value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpathfactory_set_feature(CodeRunner, Value, Value, Value)'

    /**
     * Gets the value of a feature flag.
     * @param ctxt context
     * @param fact factory
     * @param name feature name
     * @return value of passed feature
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpathfactory_get_feature(final CodeRunner ctxt,
                                                            final Value fact,
                                                            final Value name)
        throws Fail.Exception {
        try {
            final XPathFactory f = (XPathFactory) fact.asBlock().asCustom();
            return f.getFeature(name.asBlock().asString())
                ? Value.TRUE
                : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpathfactory_get_feature(CodeRunner, Value, Value)'

    /**
     * Constructs an xpath instance.
     * @param ctxt context
     * @param fact factory
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpath_make(final CodeRunner ctxt,
                                              final Value fact)
        throws Fail.Exception {
        try {
            final XPathFactory f = (XPathFactory) fact.asBlock().asCustom();
            return Cadmium.createObject(f.newXPath());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpath_make(CodeRunner, Value)'

    /**
     * Compiles an xpath expression.
     * @param ctxt context
     * @param xpath xpath instance
     * @param expr expression to compile
     * @return compiled expression
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpath_compile(final CodeRunner ctxt,
                                                 final Value xpath,
                                                 final Value expr)
        throws Fail.Exception {
        try {
            final javax.xml.xpath.XPath x =
                (javax.xml.xpath.XPath) xpath.asBlock().asCustom();
            return Cadmium.createObject(x.compile(expr.asBlock().asString()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpath_compile(CodeRunner, Value, Value)'

    /**
     * Evaluates an xpath expression.
     * @param ctxt context
     * @param xpath xpath instance
     * @param expr expression to evaluate
     * @param file evaluation context
     * @return result of evaluation
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpath_evaluate_file(final CodeRunner ctxt,
                                                       final Value xpath,
                                                       final Value expr,
                                                       final Value file)
        throws Fail.Exception {
        try {
            final javax.xml.xpath.XPath x =
                (javax.xml.xpath.XPath) xpath.asBlock().asCustom();
            final String res =
                x.evaluate(expr.asBlock().asString(),
                           ctxt.getContext().getInputStreamForPath(file));
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpath_evaluate_file(CodeRunner, Value, Value, Value)'

    /**
     * Evaluates an xpath expression.
     * @param ctxt context
     * @param xpath xpath instance
     * @param expr expression to evaluate
     * @param channel evaluation context
     * @return result of evaluation
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpath_evaluate_channel(final CodeRunner ctxt,
                                                          final Value xpath,
                                                          final Value expr,
                                                          final Value channel)
        throws Fail.Exception {
        try {
            final javax.xml.xpath.XPath x =
                (javax.xml.xpath.XPath) xpath.asBlock().asCustom();
            final Channel ch = (Channel) channel.asBlock().asCustom();
            final String res = x.evaluate(expr.asBlock().asString(), ch.asInputStream());
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpath_evaluate_channel(CodeRunner, Value, Value, Value)'

    /**
     * Evaluates an xpath expression.
     * @param ctxt context
     * @param xpath xpath instance
     * @param expr expression to evaluate
     * @param document evaluation context
     * @return result of evaluation
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpath_evaluate_document(final CodeRunner ctxt,
                                                           final Value xpath,
                                                           final Value expr,
                                                           final Value document)
        throws Fail.Exception {
        try {
            final javax.xml.xpath.XPath x =
                (javax.xml.xpath.XPath) xpath.asBlock().asCustom();
            final String res = x.evaluate(expr.asBlock().asString(),
                                          document.asBlock().asCustom());
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpath_evaluate_document(CodeRunner, Value, Value, Value)'

    /**
     * Evaluates an xpath expression.
     * @param ctxt context
     * @param expr xpath expression
     * @param file evaluation context
     * @return result of evaluation
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpath_expr_evaluate_file(final CodeRunner ctxt,
                                                            final Value expr,
                                                            final Value file)
        throws Fail.Exception {
        try {
            final XPathExpression e = (XPathExpression) expr.asBlock().asCustom();
            final String res =
                e.evaluate(ctxt.getContext().getInputStreamForPath(file));
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpath_expr_evaluate_file(CodeRunner, Value, Value)'

    /**
     * Evaluates an xpath expression.
     * @param ctxt context
     * @param expr xpath expression
     * @param channel evaluation context
     * @return result of evaluation
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpath_expr_evaluate_channel(final CodeRunner ctxt,
                                                               final Value expr,
                                                               final Value channel)
        throws Fail.Exception {
        try {
            final XPathExpression e = (XPathExpression) expr.asBlock().asCustom();
            final Channel ch = (Channel) channel.asBlock().asCustom();
            final String res = e.evaluate(ch.asInputStream());
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpath_expr_evaluate_channel(CodeRunner, Value, Value)'

    /**
     * Evaluates an xpath expression.
     * @param ctxt context
     * @param expr xpath expression
     * @param document evaluation context
     * @return result of evaluation
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpath_expr_evaluate_document(final CodeRunner ctxt,
                                                                final Value expr,
                                                                final Value document)
        throws Fail.Exception {
        try {
            final XPathExpression e = (XPathExpression) expr.asBlock().asCustom();
            final String res = e.evaluate(document.asBlock().asCustom());
            return Value.createFromBlock(Block.createString(res));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpath_expr_evaluate_document(CodeRunner, Value, Value)'

    /**
     * Resets an xpath instance.
     * @param ctxt context
     * @param xpath xpath instance
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_xpath_reset(final CodeRunner ctxt,
                                               final Value xpath)
        throws Fail.Exception {
        try {
            final javax.xml.xpath.XPath x =
                (javax.xml.xpath.XPath) xpath.asBlock().asCustom();
            x.reset();
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_xpath_reset(CodeRunner, Value)'

} // end class 'XPath'
